<template>
  <!--操作栏：编辑对话框-->
  <el-dialog :model-value="visible" @close="dialogClose" width="30%">
    <!--标题-->
    <template #header>
      <div style="font-size:18px; color:#409eff; font-weight:bold;">修改用户信息</div>
    </template>

    <el-form :model="row" ref="formRef" :rules="formRules" label-width="100px">
      <el-form-item label="登陆账号：">
        <el-input v-model="row.username" disabled></el-input>
      </el-form-item>
      <el-form-item label="用户名：" prop="name">
        <el-input v-model="row.name"></el-input>
      </el-form-item>
      <el-form-item label="用户状态：">
        <el-switch v-model="row.status" class="isShow" :active-value="'启用'" :inactive-value="'禁用'" active-color="#13ce66" inactive-color="#73767a" inline-prompt active-text="启用" inactive-text="禁用"/>
      </el-form-item>
      <el-form-item label="部门：" prop="department">
        <el-input v-model="row.department"></el-input>
      </el-form-item>
      <el-form-item label="职位：" prop="position">
        <el-input v-model="row.position"></el-input>
      </el-form-item>
      <el-form-item label="手机号码：" prop="mobile">
        <el-input v-model="row.mobile"></el-input>
      </el-form-item>
      <el-form-item label="邮箱：" prop="email">
        <el-input v-model="row.email"></el-input>
      </el-form-item>

      <!-- 配置处理人选择，通过下拉框选择-->
      <el-form-item label="角色账号：" prop="role">
        <el-select class="m-2" v-model="row.role" @click="getRoles" placeholder="请选择" style="width: 200%;">
          <el-option v-for="row in role" :key="row.id" :label="`${row.role}`" :value="row.id"></el-option>
        </el-select>
      </el-form-item>
    </el-form>

    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogClose">取消</el-button>
        <el-button type="primary" @click="dialogDepartmentEdit_btn">确定</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script>
export default {
  name: 'UserEdit',
  // 介绍父组件的值
  props: {
    visible: Boolean, // 获取dialog是否打开变量
    row: {
      type: [Object,String,Number],
      default: 0
    }
  },
  data() {
    return {
      role: '',
      formRules: {
        name: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 2, message: '用户名长度应不小于2个字符', trigger: 'blur' },
          {pattern: /[\u4e00-\u9fa5]+/, message:'用户名不符合规范，请使用中文字符',trigger:'blur'},
        ],
        department: [
          { min: 2, message: '部门长度应不小于2个字符', trigger: 'blur' },
          {pattern: /[A-Za-z0-9_\-\u4e00-\u9fa5]+/, message:'部门不符合规范，请使用中文字符',trigger:'blur'},
        ],
        position: [
          { min: 2, message: '职位长度应不小于2个字符', trigger: 'blur' },
          {pattern: /[A-Za-z0-9_\-\u4e00-\u9fa5]+/, message:'职位不符合规范，请使用中文字符',trigger:'blur'},
        ],
        mobile:[
          {required:true, message:'手机号码不能为空',trigger:'blur'},
          { min: 11, max: 11, message: '职位长度应不小于2个字符', trigger: 'blur' },
          {pattern:/^[1][35789]\d{9}$/, message:'手机号码必须要符合规范',trigger:'blur'},
        ],
        email:[
          {required:true, message:'邮箱地址不能为空',trigger:'blur'},
          {pattern:/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/, message:'邮箱地址必须要符合规范',trigger:'blur'},
        ],
      }
    }
  },
  methods: {
    // 获取部门信息
    getRoles() {
      this.$http.get('api/roles/?page_size=100').then(res => {
        this.role = res.data.data
      })
    },
    // 点击关闭，子组件通知父组件更新属性
    dialogClose() {
      this.$emit('update:visible', false) // 父组件必须使用 v-model
    },
    dialogDepartmentEdit_btn() {
      // 验证表单是否通过
      this.$refs.formRef.validate(valid => {
        if (valid) {
          this.$http
            .put('api/user/' + this.row.id + '/', this.row)
            .then(res => {
              if (res.data.code == 200) {
                // 反馈请求接口情况
                this.$message.success('修改数据成功')
                this.$parent.GetAllUsers() // 调用父组件方法，更新数据
                // 关闭弹出窗口
                this.dialogClose()
              } else {
                this.$message.info('修改数据失败')
              }
            })
            .catch(error => {
              this.$message.error('服务服务端接口请求错误！' + error)
            })
        } else {
          console.log("格式错误")
        }
      })
    }
  },
  // 监听窗口打开，
  watch: {
    visible() {
      if (this.visible) {
        // 关闭窗口不请求
        this.$http.get('api/user/' + this.row.id + '/').then(res => {
          if (res.data.code == 200) {
            // IDC机房：从对象中提取ID字段重新赋值
            this.row.role = res.data.data.role.id

            // 重新渲染编辑对话框角色
            this.getRoles()
          }
        })
      }
    }
  }
}
</script>

<style scoped></style>
